﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frmChuyencan : Form
    {
        public frmChuyencan()
        {
            InitializeComponent();
        }
        cls_DM_Chuyencan objChuyencan = new cls_DM_Chuyencan();
        private void frmChuyencan_Load(object sender, EventArgs e)
        {
            GetAll_Chuyencan();
        }
        private void GetAll_Chuyencan()
        {
            gridControl1.DataSource = objChuyencan.GetAll_Chuyencan();
        }
        private void Save_Chuyencan()
        {
            clsDM_Chuyencan objdmcc = new clsDM_Chuyencan();
            objdmcc.TenChuyenCan = gridView1.GetFocusedRowCellValue("TenChuyenCan").ToString();
            objdmcc.GhiChu = gridView1.GetFocusedRowCellValue("GhiChu").ToString();
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmcc.ChuyenCanID = 1;
                objdmcc.isChangeable = false;
                blnResult = objChuyencan.Insert(objdmcc);
            }
            else
            {
                objdmcc.ChuyenCanID = (int)gridView1.GetFocusedRowCellValue("ChuyenCanID");
                objdmcc.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objChuyencan.update(objdmcc);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Chuyencan();
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenChuyenCan").ToString()) == DialogResult.No)
                return;
            objChuyencan.delete((int)gridView1.GetFocusedRowCellValue("ChuyenCanID"));
            GetAll_Chuyencan();
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Chuyencan();
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
        }


    }
}
